require 'optparse'
require 'ostruct'
require 'boardparse' #only for ALLOWED_CHANS 
class Commopts
  def initialize comm,default_chan
    @supported_chans = ALLOWED_CHANS.join('|')
    @command = comm
    @options = OpenStruct.new
    @options.chan = default_chan #default chan for lastpost,randpost,list
    @default_chan = default_chan
  end
  attr_accessor :options
  def parse(args)
    case @command
    when "lastpost", "randpost"
      @options.section = "b"    #default section
      opts =  OptionParser.new do |opts|
        opts.banner = "Usage: #{@command} [options] /section/"
        #opts.separator ""
        opts.separator "Specific options:"
        opts.on('-c', '--chan [CHAN]', ALLOWED_CHANS, "select your chan from the list of supported chans: #{@supported_chans}") do |chan|
          @options.chan = chan
        end
        #opts.separator ""
        opts.separator "Common options:"
        opts.on_tail('-h','--help',"show this message") do 
          return opts.to_s
        end
      end
    when "list"
      @options.count = 10
      opts =  OptionParser.new do |opts|
        opts.banner = "Usage: #{@command} [options] /section/"
        opts.separator "Specific options:"
        opts.on('-c', '--chan [CHAN]', ALLOWED_CHANS, "select your chan from the list of supported chans: #{@supported_chans}") do |chan|
          @options.chan = chan
        end
        opts.on('-n', '--count [NUM]', Integer, "Specify amount of threads to list") do |num|
          @options.count = num
        end
        opts.separator "Common options:"
        opts.on_tail('-h','--help',"show this message") do 
          return opts.to_s
        end
      end
    when "googlesearch"
      @options.search_provider = "google"
      opts = OptionParser.new do |opts|
        opts.banner = "Usage: #{@command} [options] text"
        opts.separator "Common options:"
        opts.on_tail('-h','--help',"show this message") do
          return opts.to_s
        end
      end
    when "wishmaster"
      @options.defaultchan = "0chan.ru"
      opts = OptionParser.new do |opts|
        opts.banner = "Usage: #{@command} [options] args"
        opts.separator "Specific options:"
        opts.on('-d', '--defaultchan [CHAN]', ALLOWED_CHANS, "Select default chan", "List of supported chans: #{@supported_chans}") do |chan|
          @options.defaultchan = chan
        end
        opts.separator "Common options:"
        opts.on_tail('-h','--help',"show this message") do
          return opts.to_s
        end
      end
    when "version"
      #version doesn't call opts parser, this code will never execute
      opts = OptionParser.new do |opts|
        opts.banner = "Usage: #{@command} [options] nick"
        opts.separator "Common options:"
        opts.on_tail('-h','--help',"show this message") do
          return opts.to_s
        end
      end
    else
      "error"
    end

    begin
      opts.parse! args
      @options
    rescue OptionParser::InvalidOption => e
      return e.to_s
    rescue OptionParser::InvalidArgument => e
      return e.to_s
    end
  end
end
